import DynamicView from "@/layouts/DynamicView";

var DialogBox = {};

import FormBoListApi from "@/api/form/core/formBoList";
import FormPcDialog from "@/views/modules/form/core/FormPcDialog";
import FormSolutionDialog from "@/views/modules/form/core/FormSolutionDialog";
import FormTransConfigDialog from "@/views/modules/form/core/FormTransConfigDialog";
import FormQueryStartegyDialog from "@/views/modules/form/core/FormQueryStartegyDialog";
import FormRegLibDialog from "@/views/modules/form/core/FormRegLibDialog";
import FormBoListTreeDialog from "@/views/modules/form/core/FormBoListTreeDialog";
import FormBoListPreview from "@/views/modules/form/core/FormBoListPreview";
import FormBoListEdit3 from "@/views/modules/form/core/FormBoListEdit3";
import OsUserGet from "@/views/modules/user/org/OsUserGet";
import OsGroupGet from "@/views/modules/user/org/OsGroupGet";
import OsInstGet from "@/views/modules/user/org/OsInstGet";
import SysMenuAddNode from "@/views/modules/system/core/SysMenuAddNode";
import FormUserPermissionEditDialog from '@/views/modules/form/core/formPermission/FormUserPermissionEditDialog';
import SysTreeDialog from "@/views/modules/system/core/SysTreeDialog";
import FormBoListExportExcelDialog from "@/views/modules/form/core/FormBoListExportExcelDialog";
import FormBoListImportExcelDialog from "@/views/modules/form/core/FormBoListImportExcelDialog";
import ButtonFunConfig from "@/views/modules/form/core/ButtonFunConfig";
import FormBoListFieldRang from "@/views/modules/form/core/FormBoListFieldRang";
import SysMenuReleaseDialog from "@/views/modules/system/core/SysMenuReleaseDialog";
import LodopError from "@/views/modules/form/core/LodopError";

import FormBusSolutionDialog from "@/views/modules/form/core/FormBusSolutionDialog";
import FormBusSolutionShow from "@/views/modules/form/core/FormBusSolutionShow";

import {Util} from "@lowcode/jpaas-common-lib"
import store from '@/store'


import {I18Parse,CommonLang} from "@lowcode/jpaas-common-lib";
import js from '../../locales/lang/impl/assets/jsLang';
let lang = js;
let i18n=function (langKey,text) {
    return I18Parse.getI18Val(lang.js.DialogBox,langKey, text)
}

DialogBox.openLodopError = function (config) {
    var baseConf = {
        component: LodopError,
        widthHeight: ['400px', '300px'],
        title: I18Parse.getI18Val(CommonLang,'rxMsa.errorTip','错误提示')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action) {
    })
}
DialogBox.openMenuReleaseDialog = function (config) {
    var baseConf = {
        component: SysMenuReleaseDialog,
        widthHeight: ['800px', '600px'],
        title: i18n('record', '查看发布菜单记录')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action) {
    })
}


DialogBox.openFieldRangConfig = function (config, callback) {
    var baseConf = {
        component: FormBoListFieldRang,
        title: i18n('configurationData', '数据范围配置')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action, data) {
        if (action == 'ok' && callback) {
            callback(data);
        }
    })
}

DialogBox.openButtonFunConfig = function (config, callback) {
    var baseConf = {
        component: ButtonFunConfig,
        title:  i18n('configurationButton', '按钮方法配置')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action, data) {
        if (action == 'ok' && callback) {
            callback(data);
        }
    })
}

DialogBox.openPermissionUser = function (self_, eidt, callback,show,title) {
    var config = {
        curVm: self_, widthHeight: ['800px', '550px']
    }
    Object.assign(config, {
        data: {
            value: eidt,
            type: self_.type,
            dataType: self_.dataType,
            show:show,
        }
    });
    let name = title?title+ i18n('permissSet', '权限设置'):i18n('permissSet', '权限设置')
    var baseConf = {
        component: FormUserPermissionEditDialog,
        title:name
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action, data) {
        if (action != 'ok') {
            return;
        }
        if (callback) {
            callback(this.$parent, data);
        }
    });
}

DialogBox.openForm = function (config, callBack) {

    var baseConf = {
        component: FormPcDialog,
        title: i18n('formDialog', '表单对话框')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action) {
        var form = this.getForm();
        if (callBack) {
            callBack(form);
        }
    })
}

DialogBox.openMobileFormList = function (config, callBack) {

    var baseConf = {
        component: FormMobileDialog,
        title: i18n('form', '手机表单')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action) {
        var form = this.getForm();
        if (callBack) {
            callBack(form);
        }
    })
}



DialogBox.openSoution = function (config, callback) {
    var baseConf = {
        component: FormSolutionDialog,
        title: i18n('formDialog', '表单对话框')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action) {
        if (action != 'ok') return;
        var soutions = this.getSoutions();
        if (callback) {
            callback(soutions);
        }
    })
}

DialogBox.openFormTran = function (config, callback) {
    var baseConf = {
        component: FormTransConfigDialog,
        title: i18n('formTranDialog', '单据转换对话框')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action,data) {
        if (action != 'ok') return;
        if (callback) {
            callback(data);
        }
    })
}

DialogBox.openSearchView = function (config, callback) {
    var baseConf = {
        component: FormQueryStartegyDialog,
        title: i18n('searchViewDialog', '系统视图对话框')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action,data) {
        if (action != 'ok') return;
        if (callback) {
            callback(data);
        }
    })
}

DialogBox.openRegLib = function (config, callback) {
    var baseConf = {
        component: FormRegLibDialog,
        title:i18n('verification', '数据校验对话框')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action) {
        if (action != 'ok') return;
        var data = this.getData();
        if (callback) {
            callback(data);
        }
    })
}

DialogBox.openTreeList = function (config, callback) {
    var baseConf = {
        component: FormBoListTreeDialog,
        title: i18n('treeDialog', '树形对话框')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action,data) {
        if (action != 'ok') return;
        if (callback) {
            callback(this.$parent, data);
        }
    })
}

/**
 *  DialogBox.showForm({
        title:title,
        curVm: self,
        data: {alias: alias, pkId: pkId, readOnly: readOnly, parent: parent},
        max:true
      }, function (action) {
        self.onRefresh();
      });
 * @param config
 * @param callback
 */
DialogBox.showForm = function (config, callback) {
    //是否是树形表单方案
    let view =config.tree == 1 ? "modules/form/core/FormSolutionTreeShow.vue": "modules/form/core/FormSolutionShow"
    var component = this.loadView(view);
    let _showType = store.state.appSetting.showType;
    if (_showType && _showType == "newPage" && (!config.open || config.open!='window')) {//新页面打开，非弹窗类型；
        let _key  = component.name + config.urlType ;
        //获取当前打开组件的 path。component.__source (生产环境下组件path),component.__file (开发环境下path)
        let _path = component.__source || component.__file ;
        _path = _path.replace('src/views/','');
        let obj = {
            component:_path,//组件路径
            props:config.data,
            key:_key,
            title:config.title,
            cmptKey:_key + new Date().getTime()
        }
        if(config.data && config.data.alias){
            obj.key=config.data.alias;
        }
        store.commit('appSetting/setActiveKey',obj.key);
        let _navigation = store.state.appSetting.navigation;
        //如果this.navigation没有存在当前要打开的控件则返回 -1 ；
        let _isObj = _navigation.findIndex(item => { return item.key == obj.key && item.title == obj.title });
        if(_isObj == -1){
            let _arr = [..._navigation,obj];
            store.commit('appSetting/setNavigation',_arr);
        }else {
            //存在的情况下，替换；
            let _idx = null ;
            let _navArr = _navigation.concat();
            for (let i in _navArr){
                if(_navArr[i].key == obj.key){
                    _idx = i ;
                }
            }
            _navArr[_idx] = obj ;
            store.commit('appSetting/setNavigation',_navArr);
        }
    } else {//弹窗打开；
        var title = config.title ? config.title : i18n('useForm', '使用表单');
        var baseConf = {
            component: component,
            title: title
        };
        let conf = {...baseConf, ...config};
        Util.open(conf, function (action,data) {
            if(callback){
                callback(action,data);
            }

        });
    }


}

DialogBox.showBusForm = function (config, callback) {
    let _showType = store.state.appSetting.showType;

    var title = "使用业务方案"
    var baseConf = {
        component: FormBusSolutionShow,
        title: title
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action,data) {
        if(callback){
            callback(action,data);
        }

    });



}

/**
 * 打开列表对话框。
 * @param parameter {key:"对话框KEY",params:"Q_NAME__S_EQ=老王"}
 * @param config  {max:true,widthHeight:[data.width + 'px', data.height + 'px']}
 * @param callback
 */
DialogBox.dialog = function (parameter, config, callback) {
    FormBoListApi.setConf(parameter).then(res => {
        if (!res.success) return;
        var data = res.data;
        if(!config.hasOwnProperty("max")){
            if(data.isMax=="NO"){
                config.widthHeight = [data.width + 'px', data.height + 'px'];
                config.max=false;
            }
            else{
                config.max=true;
            }
        }

        let obj = {alias: parameter.key};
        let _parameter = {...obj, ...parameter}
        var baseConf = {
            component: FormBoListPreview,
            data: _parameter,
            title: data.name
        };
        let conf = {...baseConf, ...config};
        Util.open(conf, function (action, data) {
            if (action != 'ok') return;
            if (callback) {
                callback(action,data);
            }
        })
    });
}


DialogBox.editHtml = function (pkId, config,showModal) {
    var baseConf = {
        component: FormBoListEdit3,
        data: {pkId: pkId,showModal:showModal},
        title: i18n('preview', 'HTML代码预览')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action) {

    });
}


DialogBox.openDeploymenuDialog = function (parameter, config, callback) {
    var baseConf = {
        component: SysMenuAddNode,
        data: parameter,
        title: I18Parse.getI18Val(CommonLang,'rxList.deployMenu','发布菜单')
    };
    let conf = {...baseConf, ...config};
    if(conf.key){
        conf.mKey = conf.key ;
    }
    Util.open(conf, function (action, data) {
        if (action != 'ok') return;
        if (callback) {
            callback(data);
        }
    })
}

DialogBox.getUserDetail = function (self, userId) {
    var baseConf = {
        "curVm": self,
        component: OsUserGet,
        title: i18n('userInfo', '用户信息'),
        widthHeight: ['1200px', '90vh'],
        "data": {
            "pkId": userId, callback: function (data) {
                console.info(data);
            }
        }
    };
    Util.open(baseConf, function (action) {

    });
}

DialogBox.getGroupDetail = function (self, groupId) {
    var baseConf = {
        "curVm": self,
        component: OsGroupGet,
        title:  i18n('groupInfo', '用户组信息'),
        widthHeight: ['1200px', '90vh'],
        "data": {
            "pkId": groupId, callback: function (data) {
                console.info(data);
            }
        }
    };
    Util.open(baseConf, function (action) {

    });
}

DialogBox.getInstDetail = function (self, instId) {
    var baseConf = {
        "curVm": self,
        component: OsInstGet,
        title: i18n('osInstInfo', '机构信息'),
        widthHeight: ['1200px', '90vh'],
        "data": {
            "pkId": instId
        }
    };
    Util.open(baseConf, function (action) {
    });
}

DialogBox.openSysTreeDialog = function (config, callback) {
    var baseConf = {
        component: SysTreeDialog,
        title: i18n('dialogCategory', '分类对话框')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action) {
        if (action != 'ok') return;
        var data = this.getData();
        if (callback) {
            callback(data);
        }
    })
}

DialogBox.openExportExcelDialog = function (config,callback) {
    var name = config.data.name;
    var baseConf = {
        component: FormBoListExportExcelDialog,
        title: name + i18n('formExport', '导出EXCEL表单'),
        max:true
    }
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action,data) {
        if(callback){
            callback(action,data);
        }
    })
}

DialogBox.openImportExcelDialog = function (config, callback) {
    var name = config.data.name;
    var baseConf = {
        component: FormBoListImportExcelDialog,
        title: name + i18n('import','导入Excel')
    }
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action) {
        if (action != 'ok') return;
        if (callback) {
            callback();
        }
    })
}

DialogBox.loadView = (view) => { // 路由懒加载
    return require(`@/views/${view}`).default;
    //return () => import(`@/views/${view}`)
}

/**
 * 打开自定义组件。
 *
 * @param config {
 *   component:"组件的URL",
 *   title:"标题",
 *   max:true,
 *   curVm:this,
 *   widthHeight:['800px','600px'],
 *   data:{user:{name:"ray",address:"gz"}},
 * }
 * @param callBack 回调方法 function(action,data){
 *
 * }
 */
DialogBox.openComponent = function (config, callBack) {
    var component = DialogBox.loadView(config.component);
    var title = config.title || i18n('custom','自定义对话框');

    var conf = {
        component: DynamicView,
        curVm: config.curVm,
        title: title,
        data: {currentView: component, data: config.data}
    };
    if (config.widthHeight) {
        conf.widthHeight = config.widthHeight;
    } else {
        conf.max = true;
    }
    if (config.cancel) {
        conf.cancel = config.cancel;
    }

    Util.open(conf, function (action, data) {
        if (callBack) {
            let func=callBack.bind(this);
            func(action, data);
        }
    })
}

/**
 * 启动流程。
 * @param config
 * var data={defId:"1418459535940534274",formData:{address:"gz"}};
 var config={
        curVm:this,
        max:true,
        data: data
    }
 * @param callback (action,data)
 */
DialogBox.openStartFlow = function (config,callback) {
    let compoment=this.loadView('modules/bpm/core/BpmInstStart');
    var baseConf = {
        component: compoment,
        title:i18n('startProcessBtn','启动流程')
    }
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action,data) {
        if (callback) {
            callback(action,data);
        }
    })
}

DialogBox.openBusSolutionDialog= function (config, callback) {
    var baseConf = {
        component: FormBusSolutionDialog,
        title: i18n('dialogBusiness','业务方案对话框')
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action,data) {
        if (action != 'ok') return;
        if (callback) {
            callback(data);
        }
    })
}

DialogBox.showBusSolutionForm = function (config, callback) {
    var baseConf = {
        component: FormBusSolutionShow,
        title: config.title
    };
    let conf = {...baseConf, ...config};
    Util.open(conf, function (action,data) {
        if (action != 'ok') return;
        if (callback) {
            callback(data);
        }
    })
}


export default DialogBox;
